﻿#include <QPainter>
#include <QDebug>

#include "CPlcItemLinkLine.h"

CPlcItemLinkLine::CPlcItemLinkLine(const QRectF &rect,
                                   const int8_t type,
                                   const QString &str,
                                   IPlcBaseItem *parent)
    : IPlcBaseItem(parent)
{
    mVariableNum  = str;
    mSelectActive = false;
    mSwitchOn     = false;
    mBranch       = NULL;
    mType         = type;
    setRect(rect);
}

CPlcItemLinkLine::~CPlcItemLinkLine()
{
    //如果当前为选中状态
    if (mSelectActive) setOnlySelectItem(NULL);
}

QRectF CPlcItemLinkLine::getRect()
{
    return mRect;
}

void CPlcItemLinkLine::setRect(const QRectF &rect)
{
    if (mRect == rect) return;
    //准备调整几何边界
    prepareGeometryChange();
    mRect         = rect;
    mBoundingRect = QRectF();
    // 缩放比例
    mdheightScale = mRect.height() / 55.0;
    //重绘
    update();
}

IPlcBaseItem *CPlcItemLinkLine::getBranch()
{
    return mBranch;
}

void CPlcItemLinkLine::setBranch(IPlcBaseItem *item)
{
    mBranch = item;
}

void CPlcItemLinkLine::deleteBranch()
{
    mBranch = NULL;
}

void CPlcItemLinkLine::setSelectActive(bool status)
{
    //复位之前选中的图元
    if (this != getOnlySelectItem())
    {
        if (getOnlySelectItem() != NULL)
            getOnlySelectItem()->setSelectActive(false);
    }

    if (mSelectActive == status) return;

    mSelectActive = status;
    status ? setOnlySelectItem(this) : setOnlySelectItem(NULL);
    //重绘图元
    update();
}

void CPlcItemLinkLine::setSwitchOn(bool value)
{
    if (mSwitchOn == value) return;

    mSwitchOn = value;
    //重绘图元
    update();
}

int8_t CPlcItemLinkLine::getItemType()
{
    return mType;
}

QRectF CPlcItemLinkLine::boundingRect() const
{
    if (mBoundingRect.isNull()) mBoundingRect = mRect;

    return mBoundingRect;
}

void CPlcItemLinkLine::paint(QPainter *painter,
                             const QStyleOptionGraphicsItem *option,
                             QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    // 设置反走样
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setRenderHint(QPainter::TextAntialiasing, true);

    QBrush blackBrush(Qt::black);
    QBrush blueBrush(QColor(2, 167, 240));
    QPen pen;
    //绘制选中状态
    if (mSelectActive)
    {
        //设置画笔样式为虚线
        pen.setStyle(Qt::DashLine);
        pen.setWidth(1);         //线宽1
        pen.setBrush(Qt::red);   //颜色蓝色

        painter->setPen(pen);
        painter->drawRect(mRect);   //画矩形
    }

    //设置画笔为实线
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(0);   //线宽1
    //根据信号接通状态设置导线颜色
    if (mSwitchOn)
    {
        pen.setBrush(blueBrush);   //绿色
    }
    else
    {
        pen.setBrush(blackBrush);   //黑色
    }
    painter->setPen(pen);
    double x1 = mRect.left();
    double y1 = mRect.top() + mdheightScale * 35;
    double x2 = mRect.right();
    painter->drawLine(x1, y1, x2, y1);   //画直线
}

bool CPlcItemLinkLine::getSelectActiveEnb()
{
    return true;
}

bool CPlcItemLinkLine::getSelectActive()
{
    return mSelectActive;
}
